FROM php:8.3-fpm

# 配置清华源镜像加速（中国大陆优化）
RUN set -ex; \
    # 兼容Debian 12的新格式sources.list \
    if [ -f /etc/apt/sources.list.d/debian.sources ]; then \
        sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources; \
        sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources; \
    fi; \
    # 兼容旧版本的sources.list \
    if [ -f /etc/apt/sources.list ]; then \
        sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list; \
        sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list; \
    fi

# 安装系统依赖和 OpenJRE 17
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libicu-dev \
    libpq-dev \
    redis-tools \
    supervisor \
    cron \
    nano \
    netcat-traditional \
    libxml2-dev \
    openjdk-17-jre \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 安装PHP扩展
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip intl opcache calendar

# 安装Redis扩展
RUN pecl install redis && docker-php-ext-enable redis

# 安装Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# 配置PHP
COPY dockerfiles/php/php.ini /usr/local/etc/php/conf.d/custom.ini
COPY dockerfiles/php/www.conf /usr/local/etc/php-fpm.d/www.conf

# 配置Supervisor
COPY dockerfiles/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 配置Cron
COPY dockerfiles/supervisor/crontab /etc/cron.d/laravel-cron
RUN chmod 0644 /etc/cron.d/laravel-cron && crontab /etc/cron.d/laravel-cron

# 创建工作目录
WORKDIR /www/cert_manager

# 复制入口脚本
COPY dockerfiles/php/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 暴露端口
EXPOSE 9000

# 启动脚本
ENTRYPOINT ["/entrypoint.sh"]